package main

import (
	"flag"
	"fmt"
	"goED/util"
)

func test() {
	//content, err := ioutil.ReadFile("C:\\Users\\61778\\Desktop\\HelloWorld.class")
	strContent := util.ReadFileToStr("C:\\METSIM\\example\\gascleaning_1.5_30.sfw")
	//fmt.Println(strContent)
	Encrypt, _ := util.AesEcpt.AesBase64Encrypt(strContent)
	//fmt.Printf("Encrypt: %v\n", Encrypt)
	Decrypt, _ := util.AesEcpt.AesBase64Decrypt(Encrypt)
	//fmt.Printf("Decrypt: %v\n", Decrypt)
	fmt.Printf("明文长度: %v\n解密后长度: %v\n", len(strContent), len(Decrypt))
}

var mode = flag.String("mode", "de", "加密或者解密  en de")
var path = flag.String("path", "", "文件路径")
var outPath = flag.String("outPath", "", "指定输出文件路径")
var key = flag.String("key", "", "加密秘钥-限制大小写字母数字16位 可以不指定")

func main() {
	flag.Parse()
	//验证操作文件路径是否有效
	flag, message := util.PathExists(*path)
	if !flag {
		fmt.Printf(message)
	} else {
		// 输出路径设置
		var out string = *path
		var modeName string
		if len(*outPath) != 0 {
			out = *outPath
		}
		//验证秘钥是否有效
		if len(*key) == 16 {
			util.InitBykey(*key)
		} else if (len(*key) > 0) && (len(*key) < 16) {
			fmt.Printf("秘钥限制大小写字母数字16位")
		}
		//验证mode 输入是否正确
		if *mode != "en" && *mode != "de" {
			fmt.Printf("mode 只能是 en 或者 de")
			//执行加密
		} else if *mode == "en" {
			modeName = "加密"
			util.EnFileToOutPath(*path, out)
			//执行解密
		} else if *mode == "de" {
			modeName = "解密"
			util.DeFileToOutPath(*path, out)
		}
		fmt.Println(modeName + "成功,被" + modeName + "文件是在: " + *path + " " + modeName + "后文件生产在：" + out)
	}

	//test()
	// TestGenFile("C:\\METSIM\\example\\gascleaning_1.5_30.sfw",
	// 	"C:\\Users\\61778\\Desktop\\testGoED\\gascleaning_1.5_30.sfw.EN",
	// 	"C:\\Users\\61778\\Desktop\\testGoED\\gascleaning_1.5_30.sfw")
	// EnFile("C:\\Users\\61778\\Desktop\\HelloWorld.class")
	// DeFile("C:\\Users\\61778\\Desktop\\HelloWorld.class")

	// cmd := exec.Command("cmd", "/C", "C:\\METSIM\\METSIM.exe",
	// 	"MOD=C:\\Users\\61778\\Desktop\\testGoED\\gascleaning_1.5_30.sfw", "SIL=1")
	// var stdout, stderr bytes.Buffer
	// cmd.Stdout = &stdout
	// cmd.Stderr = &stderr
	// err := cmd.Run()
	// outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes())
	// fmt.Printf("out:\n%s\nerr:\n%s\n", outStr, errStr)
	// if err != nil {
	// 	log.Fatalf("cmd.Run() failed with %s\n", err)
	// }
	// cmd := exec.Command("cmd", "/C", "C:\\METSIM\\METSIM.exe",
	// 	"MOD=C:\\Users\\61778\\Desktop\\testGoED\\gascleaning_1.5_30.sfw", "SIL=1")
	// cmd.Start()

}
